package com.lc.admin.service.impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lc.admin.base.PageResponse;
import com.lc.admin.bean.Reservation;
import com.lc.admin.mapper.ReservationMapper;
import com.lc.admin.service.ReservationService;
import com.lc.admin.utils.DateUtils;
import com.lc.admin.utils.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ReservationServiceImpl implements ReservationService {

    @Autowired
    ReservationMapper reservationMapper;
    @Override
    public PageResponse<Reservation> listByPage(Integer page, Integer pageSize, Reservation reservation) {
        PageHelper.startPage(page,pageSize);
        List<Reservation> list = reservationMapper.listByPage(reservation);
        Page pageData = (Page) list;
        PageResponse pageResponse = new PageResponse(pageData);
        pageResponse.setRecords(pageData.getResult());
        return pageResponse;
    }

    @Override
    public int update(Reservation reservation) {
        checkOnceOnAmOrPm(reservation);
        checkCountLimit(reservation);
        int update = reservationMapper.update(reservation);
        return update;
    }

    @Override
    public void save(Reservation reservation) {
        // 如果时间是今天以前的日期，预约失败
//        if(reservation.getTime().before(new Date())) {
//            throw new RuntimeException("预约时间不能早于当前时间");
//        }

        checkOnceOnAmOrPm(reservation);

        // 2、查询用户当天取消预约的次数>=3，禁止预约
        checkCountLimit(reservation);

        reservation.setStatus(0);
        Long uid = UserThreadLocal.getUserId();
        reservation.setCreateBy(uid);
        reservationMapper.save(reservation);
    }

    private void checkCountLimit(Reservation reservation) {
        if(null == reservation.getTime()) {
            return;
        }
        String date = DateUtils.getDate(reservation.getTime());
        Date startTime = DateUtils.parse(date + " 08:30:00");
        Date endTime = DateUtils.parse(date + " 18:00:00");
        reservation.setStartTime(startTime);
        reservation.setEndTime(endTime);
        int countLimit = reservationMapper.queryCountByDate(reservation);

        if(countLimit>=3) {
            throw new RuntimeException("今天取消超过3次，禁止预约");
        }
    }

    private void checkOnceOnAmOrPm(Reservation reservation) {
        if(null == reservation.getTime()) {
            return;
        }
        // 1、同一时间段同一手机号只能预约一次    2025-07-10 09:00:00   13800000000   查询上午或下午是否以及预约过了
        // select count(id) from reservation where time between '2025-07-10 08:30:00' and '2025-07-10 18:00:00' and mobile = '13800000000'
        String date = DateUtils.getDate(reservation.getTime());
        String amPm = DateUtils.getAmPm(reservation.getTime());
        if("am".equalsIgnoreCase(amPm)) {
            Date startTime = DateUtils.parse(date + " 08:30:00");
            Date endTime = DateUtils.parse(date + " 12:00:00");
            reservation.setStartTime(startTime);
            reservation.setEndTime(endTime);
        } else {
            Date startTime = DateUtils.parse(date + " 12:30:00");
            Date endTime = DateUtils.parse(date + " 18:00:00");
            reservation.setStartTime(startTime);
            reservation.setEndTime(endTime);
        }
        int count = reservationMapper.queryCountByMobileAndTime(reservation);

        if(count > 0) {
            throw new RuntimeException("同一时间段同一手机号只能预约一次");
        }
    }
}
